package com.zhongkai.app.controller;

import com.google.gson.Gson;
import com.mysql.jdbc.StringUtils;
import com.zhongkai.FastDFSClient;
import com.zhongkai.app.service.AppBasicService;
import com.zhongkai.model.utils.ZhongkaiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 图片上传到图片服务器
 */
@Controller
public class PictureController {

    @Autowired
    private AppBasicService appBasicService;
    //返回utf-8编码的数据



    //返回utf-8编码的数据
    @RequestMapping(value = "/app_uploa/img2", produces = MediaType.TEXT_HTML_VALUE + ";charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public String uploaFile(@RequestParam("upload_file") MultipartFile upload_file) {

        try {
            //把图片上传的图片服务器


            FastDFSClient fastDFSClient = new FastDFSClient("classpath:client.conf");
            //FastDFSClient fastDFSClient = new FastDFSClient("D:\\aaa\\client.conf");
            if (upload_file == null) {
                System.out.println("null");
            }
/*            for (MultipartFile multipartFile : uploadFile) {
                String originalFilename = multipartFile.getOriginalFilename();
                String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
                String url = fastDFSClient.uploadFile(multipartFile.getBytes(), extName);
                //补充为完整的url
                url = "http://111.61.64.62:8001/" + url;
            }*/

            //取文件扩展名
            String originalFilename = upload_file.getOriginalFilename();

            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            //得到一个图片的地址和文件名
            String url = fastDFSClient.uploadFile(upload_file.getBytes(), extName);
            //补充为完整的url
            url = "http://111.61.64.62:8001/" + url;
            //封装到map中返回
            Map result = new HashMap();
            result.put("error", 0);
            result.put("url", url);
            return new Gson().toJson(result);
        } catch (Exception e) {
            e.printStackTrace();
            Map result = new HashMap();
            result.put("error", 1);
            result.put("message", "图片上传失败");
            return new Gson().toJson(result);
        }
    }


    /**
     * APP修改头像
     * @param app_uuid 用户uuid
     * @param file 文件流
     */
    @RequestMapping(value = "/app_uploa/img", produces = MediaType.TEXT_HTML_VALUE + ";charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public String uploaFile(@RequestParam("files") CommonsMultipartFile file, String app_uuid) {

        //response.setHeader("Access-Control-Allow-Methods", "*");
/*
        if (httpServletRequest.getMethod().equalsIgnoreCase("OPTIONS")) {
            System.out.println("200执行");
            response.setStatus(200);
            return ZhongkaiResult.ok();
        }
*/



        if (StringUtils.isNullOrEmpty(app_uuid)) {
           // return ZhongkaiResult.build(500, "参数不完整");
            return "失败";
        }


        try {


            FastDFSClient fastDFSClient = new FastDFSClient("classpath:client.conf"); //把图片上传的图片服务器
            //FastDFSClient fastDFSClient = new FastDFSClient("D:\\aaa\\client.conf");

            if (file.isEmpty()) {
              //  return ZhongkaiResult.build(500, "上传文件不能为空");
                return "失败";
            }
/*            for (MultipartFile multipartFile : uploadFile) {
                String originalFilename = multipartFile.getOriginalFilename();
                String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
                String url = fastDFSClient.uploadFile(multipartFile.getBytes(), extName);
                //补充为完整的url
                url = "http://111.61.64.62:8001/" + url;
            }*/


            String originalFilename = file.getOriginalFilename();//取文件名以及扩展名

            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1); //取出文件名

            String url = fastDFSClient.uploadFile(file.getBytes(), extName);//得到一个图片的地址和文件名

            url = "http://111.61.64.62:8001/" + url;//补充为完整的url-

            //uploadFile.getOriginalFilename()


            //  ZhongkaiResult result = versionsService.uploadSystem(ver, url, filePath+originalFilename,"111","111");
            ZhongkaiResult result1 = appBasicService.updataImg(app_uuid, url);
            Map result = new HashMap();

            if (result1.getStatus() == 200){
                result.put("status", 200);
                result.put("url", url);
                return new Gson().toJson(result);
            }else {
                result.put("status", 400);
                result.put("url", url);
                return new Gson().toJson(result);
            }

        } catch (Exception e) {
            e.printStackTrace();
            Map result = new HashMap();
            result.put("status", 400);
            result.put("url", "失败");
            return new Gson().toJson(result);
        }

    }

    /**
     * 上传apk
     * @param app_ver app版本号
     * @param app_code app版本号标识
     * @param file 文件流
     */
    @RequestMapping(value = "/app/apk_up")
    @ResponseBody
    public ZhongkaiResult uploaApk(@RequestParam("file") CommonsMultipartFile file, String app_ver,String app_code) {

        //response.setHeader("Access-Control-Allow-Methods", "*");
/*
        if (httpServletRequest.getMethod().equalsIgnoreCase("OPTIONS")) {
            System.out.println("200执行");
            response.setStatus(200);
            return ZhongkaiResult.ok();
        }
*/



        if (StringUtils.isNullOrEmpty(app_ver) || StringUtils.isNullOrEmpty(app_code)  || file == null) {
            return ZhongkaiResult.build(500, "参数不完整");
        }


        try {


            FastDFSClient fastDFSClient = new FastDFSClient("classpath:client.conf"); //把图片上传的图片服务器
            //FastDFSClient fastDFSClient = new FastDFSClient("D:\\aaa\\client.conf");

            if (file.isEmpty()) {
                  return ZhongkaiResult.build(500, "上传文件不能为空");
            }
/*            for (MultipartFile multipartFile : uploadFile) {
                String originalFilename = multipartFile.getOriginalFilename();
                String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
                String url = fastDFSClient.uploadFile(multipartFile.getBytes(), extName);
                //补充为完整的url
                url = "http://111.61.64.62:8001/" + url;
            }*/


            String originalFilename = file.getOriginalFilename();//取文件名以及扩展名

            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1); //取出文件名

            String url = fastDFSClient.uploadFile(file.getBytes(), extName);//得到一个图片的地址和文件名

            url = "http://111.61.64.62:8001/" + url;//补充为完整的url-

            //uploadFile.getOriginalFilename()


            //  ZhongkaiResult result = versionsService.uploadSystem(ver, url, filePath+originalFilename,"111","111");
            ZhongkaiResult result1 = appBasicService.apkup(app_ver, Integer.parseInt(app_code),url);

            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();

            return ZhongkaiResult.build(500,"失败");
        }

    }


    /**
     * 修改APP姓名
     *
     * @param app_uuid 用户uuid
     * @param app_name 用户姓名
     */
    @RequestMapping(value = "/app_updata/name", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult updataName(String app_uuid, String app_name) {

        ZhongkaiResult result = appBasicService.updataName(app_uuid,app_name);

        return result;
    }



    /**
     * 修改APP地区
     *
     * @param app_uuid 用户uuid
     * @param province 省
     * @param city     市
     * @param country  区
     * @param town     乡镇 可为空
     * @param address  详细地址
     */
    @RequestMapping(value = "/app_updata/address", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult updataAddress(String app_uuid, String province, String city,
                                        String country, String town, String address) {

        ZhongkaiResult result = appBasicService.updataAddress(app_uuid,province,city,country,town,address);

        return result;
    }




    /**
     * 修改密码
     *
     * @param app_uuid   用户uuid
     * @param password   新密码
     * @param passworded 旧密码
     */
    @RequestMapping(value = "/app/update_password", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult updataPassword2(String app_uuid, String passworded, String password) {

        ZhongkaiResult result = appBasicService.updataPassword2(app_uuid,passworded,password);

        return result;
    }



    /**
     * 修改绑定手机号
     *
     * @param app_uuid 用户uuid
     * @param phone    用户姓名
     * @param code     验证码
     */
    @RequestMapping(value = "/app_updata/phone", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult updataPhone(String app_uuid, String phone, String code) {

        ZhongkaiResult result = appBasicService.updataPhone(app_uuid,phone,code);

        return result;
    }


    /**
     * 获取公司联系电话
     *
     */
    @RequestMapping(value = "/app/get_phone", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult getPhone() {

        ZhongkaiResult result = appBasicService.getPhone();

        return result;
    }



    /**
     * 获取公司协议
     */
    @RequestMapping(value = "/app/get_userxy", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult getUsetXy() {

        ZhongkaiResult result = appBasicService.getUsetXy();

        return result;
    }

    /**
     * 获取最新apk
     */
    @RequestMapping(value = "/app/get_apk_ver", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult apkVer() {

        ZhongkaiResult result = appBasicService.apkVer();

        return result;
    }


    /**
     * 客户留言
     *
     * @param app_uuid      用户uuid
     * @param mess_contents 内容
     */
    @RequestMapping(value = "/app_uploa/mess_center", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult messCenter(String app_uuid, String mess_contents) {

        ZhongkaiResult result = appBasicService.messCenter(app_uuid,mess_contents);

        return result;
    }




    /**
     * 获取用户信息
     *
     * @param app_uuid      用户uuid
     */
    @RequestMapping(value = "/app_user/datas", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult getUserData(String app_uuid) {

        ZhongkaiResult result = appBasicService.getUserData(app_uuid);

        return result;
    }

}